jQuery(function(){
    // 获取档口前21
    getBrandTop21();
    // 获取实力商家
    getStrengthBrand();
});

/**
 * 获取杭州地区排名前21的档口
 */
function getBrandTop21() {
    var url = '/hz/brandRank/top21';
    jQuery.ajax({
        url: url,
        type: 'get',
        dataType: 'json',
        success: function (data) {
            if (data !== undefined && data!== null && data.length > 0) {

                // 将数据分成3段
                var listWpArr = [];
                for (var i = 0; i < 3; i++) {
                    listWpArr.push(data.splice(0, 7));
                }

                var _html = '';
                for (var j = 0, len = listWpArr.length; j < len; j++) {

                    var bArr = listWpArr[j];
                    _html += "<div class='listWp"+ (j !== 0 ? ' hide' : '') +"'>";
                    for (var k = 0, bLen = bArr.length; k < bLen; k++) {
                        var imageUrl =  webImgUrl + bArr[k].image;

                        _html +=
                            "<a href='/brand-"+ bArr[k].brandId +".html' target='_blank' class='listItem'>" +
                            "   <div class='phIto'>" +
                            "       <img src='"+ imageUrl +"' alt=''/>" +
                            getBrandRank(bArr[k]) +
                            "   </div>" +
                            "   <div class='rigMie'>" +
                            "       <div class='kel'>"+ bArr[k].brandName +"</div>" +
                            "       <div class='zizeBox'>"+ bArr[k].mallCnName + bArr[k].floorName + bArr[k].brandCode +"</div>" +
                            "   </div>" +
                            "</a>"
                    }
                    _html += "</div>";
                }

                jQuery("#powerMch .tab_box").html(_html);
                jQuery("#powerMch .tab_box img").lazyload({ effect: "show", failure_limit: 10 });
                jQuery('#powerMch').Tabs({
                    auto:3000,
                    create:true,
                    addClassDiv:'focusWarp Potop'
                });
            }
        }
    });
}

function getBrandRank(brand) {
    if (brand.rowNum > 3) {
        return "";
    }

    return "<span class='rank powerrankNum"+ brand.rowNum +"'></span>";
}

/**
 * 获取杭州地区实力商家（含广告）
 */
function getStrengthBrand() {
    jQuery.ajax({
        url: '/hz/strengthBrand',
        type: 'get',
        dataType: 'json',
        success: function (data) {
            if (data !== undefined && data!== null && data.length > 0) {
                var _html = "";

                for (var i = 0, len = data.length; i < len; i++) {
                    var imageUrl = webImgUrl + data[i].image;

                    _html +=
                        "<div class='powerItem'>" +
                        "   <a href='/brand-"+ data[i].id +".html' target='_blank'>" +
                        "       <img src='"+ imageUrl +"' alt='" + data[i].name + "' class='powBig'>" +
                        "   </a>" +
                        "   <div class='nameShop'><a href='/brand-"+ data[i].id +".html' target='_blank'>"+ data[i].name +"</a></div>" +
                        "   <div class='textdkg'>"+ transformBrandYear(data[i].brandYear) +"年店&nbsp;·&nbsp;主营"+ getMainProduct(data[i].mainProduct) +"</div>" +
                        "</div>";
                }

                jQuery(".outWpad").html(_html);
                jQuery(".outWpad img").lazyload({ effect: "show", failure_limit: 10 });
            }
        }
    });
}

/**
 * 年份数字转中文
 *
 * @param brandYear
 * returns {string}
 */
function transformBrandYear(brandYear) {
    if (brandYear === 0) {
        return "";
    }

    var arr = ['一', '二', '三', '四', '五', '六', '七', '八', '九', '十',
        '十一', '十二', '十三', '十四', '十五', '十六', '十七', '十八', '十九', '二十',
        '二十一', '二十二', '二十三', '二十四', '二十五', '二十六', '二十七', '二十八', '二十九', '三十'];

    return arr[brandYear - 1] || '';
}

/**
 * 获取品牌第一个主营项
 *
 * @param mainProduct
 * @returns {string}
 */
function getMainProduct(mainProduct) {
    return (mainProduct || "").split(",")[0];
}